package frame.networks.tables;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import model.components.networks.network.PhysicalNetwork;
import frame.MopraFrame;
import frame.networks.viewers.PhysicalNetworksVisualizationViewersTabbedPane;

@SuppressWarnings("serial")
public class PhysicalNetworksTable extends JTable implements MouseListener
{
	private List< PhysicalNetwork > physicalNetworks;
	public List< PhysicalNetwork > getPhysicalNetworks(){ return physicalNetworks; }
	public void setPhysicalNetworks( List< PhysicalNetwork > physicalNetworks ){ this.physicalNetworks = physicalNetworks; }
	
	private MopraFrame mopraFrame;
	public MopraFrame getFrame(){ return mopraFrame; }
	public void setFrame( MopraFrame mopraFrame ){ this.mopraFrame = mopraFrame; }
	
	public PhysicalNetworksTable( List< PhysicalNetwork > physicalNetworks, MopraFrame mopraFrame )
	{
		this.setPhysicalNetworks( physicalNetworks );
		this.setFrame( mopraFrame );
		
		PhysicalNetworksTableModel physicalNetworksTableModel = new PhysicalNetworksTableModel( new String[]{ "Networks", "State" }, 0, physicalNetworks );
		
		for( PhysicalNetwork physicalNetwork: this.getPhysicalNetworks() )
			physicalNetworksTableModel.addRow( new Object[]{ physicalNetwork.getName(), physicalNetwork.getState() } );
		
		this.setModel( physicalNetworksTableModel );
		this.setDefaultRenderer( Object.class, new TableCellRenderer() );
		this.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
		this.addMouseListener( this );
	}
	
	@Override
	public void mouseClicked( MouseEvent mouseEvent )
	{
		int selectedRowIndex = ( ( JTable )mouseEvent.getSource() ).getSelectedRow();
		String physicalNetworkName = ( String )this.getModel().getValueAt( selectedRowIndex, 0 );
		
		for( PhysicalNetwork physicalNetwork: this.getPhysicalNetworks() )
			if( physicalNetwork.getName().equals( physicalNetworkName ) )
			{
				PhysicalNetworksVisualizationViewersTabbedPane physicalNetworksVisualizationViewersTabbedPane = this.getFrame().getNetworksPanel().getPhysicalNetworksVisualizationViewersTabbedPane();
				physicalNetworksVisualizationViewersTabbedPane.setSelectedIndex( physicalNetworksVisualizationViewersTabbedPane.indexOfTab( physicalNetwork.getName() ) );
				break;
			}
	}
	@Override
	public void mouseReleased( MouseEvent mouseEvent ){}
	@Override
	public void mouseEntered( MouseEvent mouseEvent ){}
	@Override
	public void mouseExited( MouseEvent mouseEvent ){}
	@Override
	public void mousePressed( MouseEvent mouseEvent ){}
	
}
